package com.es.controller;

import com.es.dto.request.UserSearchReq;
import com.es.entity.User;
import com.es.repository.UserRepository;
import com.es.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/es/user")
public class UserController {

    private final UserRepository userRepository;
    private final UserService userService;

    public UserController(UserRepository userRepository, UserService userService) {
        this.userRepository = userRepository;
        this.userService = userService;
    }

    @PostMapping("/add")
    public User add(@RequestBody User user) {
        return userRepository.save(user);
    }

    @GetMapping("{id}")
    public User findById(@PathVariable String id) {
        return userRepository.findById(id).orElseGet(null);
    }

    @GetMapping("/groupByGender")
    public Map<String, Integer> groupByGender() {
        return userService.groupByGender();
    }

    @GetMapping("/findByCar")
    public List<User> findByCar(UserSearchReq searchReq) {
        return userService.findByCar(searchReq);
    }

}
